import uniqueId from '@form-create/utils/lib/unique';
import {localeProps, makeRequiredRule} from '../../utils';

const label = '附件';
const name = 'jd-upload';

export default {
    icon: 'icon-upload-file',
    label,
    name,
    rule({t}) {
        return {
            type: 'Jd-Upload',
            field: 'FJSC',
            title: '附件',
            info: '',
            $required: false,
            validate: false,
            props: {
                action: '',
                onSuccess(res, file) {
                    file.url = res.data.url;
                }
            }
        };
    },
    watch: {
        'uploadType': function(val){
            if (val.value=='image'){
                val.api.rule[2].hidden = true;
                val.api.rule[3].hidden = false;
                val.api.rule[4].value = '请上传大小不超过10MB，格式为 png/jpg 的文件';
                val.api.rule[5].value = 'image/jpeg,image/png';
            } else {
                val.api.rule[2].hidden = false;
                val.api.rule[3].hidden = true;
                val.api.rule[4].value = '请上传大小不超过10MB，格式为 doc/xls/ppt/pdf 的文件';
                val.api.rule[5].value = 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.template,application/pdf,application/x-zip-compressed';
            }
        },
        'showTips': function(val){
            if (val.value){
                val.api.rule[4].hidden = false;
            } else {
                val.api.rule[4].hidden = true;
            }
        },
    },
    props(_, {t}) {
        return localeProps(t, name + '.props', 
            [
                makeRequiredRule(),
                {
                    type: 'row',
                    props: {gutter: 5},
                    children: [
                        {
                            type: 'col',
                            props: {span: 12},
                            style: {width: '130px'},
                            children: [
                                {type: 'switch', field: 'showTips', title: '上传说明',value: true}
                            ]
                        },
                        {
                            type: 'col',
                            props: {span: 12},
                            children: [
                                {
                                    type: 'select',
                                    field: 'uploadType',
                                    title: '上传类型',
                                    style: {width: '125px'},
                                    value: 'file',
                                    options: [
                                        {label: '图片', value: 'image'}, 
                                        {label: '文件', value: 'file'}
                                    ],
                                }
                            ]
                        }
                    ]
                },
                {
                    type: 'row',
                    props: {gutter: 5},
                    children: [
                        {
                            type: 'col',
                            props: {span: 12},
                            style: { width: '100px'},
                            children: [
                                {type: 'JdIconSelect', field: 'icon', title: '按钮图标'} 
                            ]
                        },
                        {
                            type: 'col',
                            props: {span: 12},
                            children: [
                                {type: 'input', field: 'title', title: '按钮文字', value: '选取附件'},
                            ]
                        }
                    ]
                },
                {
                    type: 'row',
                    props: {gutter: 5},
                    hidden: true,
                    children: [
                        {
                            type: 'col',
                            props: {span: 12},
                            children: [
                                {type: 'inputNumber', field: 'width', title: '宽度(px)', value: '100', props: {min:100,max:300}},
                            ]
                        },
                        {
                            type: 'col',
                            props: {span: 12},
                            children: [
                                {type: 'inputNumber', field: 'height', title: '高度(px)', value: '100', props: {min:100,max:300}},
                            ]
                        }
                    ]
                },
                {type: 'input', field: 'tips', title: '说明文字', value: '请上传大小不超过10MB，格式为 doc/xls/ppt/pdf的文件', props: { type:'textarea', rows: 5 }},
                {
                    type: 'select', 
                    field: 'accept',
                    title: '允许格式',
                    hidden: true,
                    value: 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.template,application/pdf,application/x-zip-compressed',
                    options: [
                        {label: '图片', value: 'image/jpeg,image/png'}, 
                        {label: '文件', value: 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.template,application/pdf,application/x-zip-compressed'}
                    ],
                },
                {
                    type: 'row',
                    props: {gutter: 5},
                    children: [
                        {
                            type: 'col',
                            props: {span: 12},
                            style: {width: '120px'},
                            children: [
                                {type: 'switch', field: 'multiple', title: '允许多选'}
                            ]
                        },
                        {
                            type: 'col',
                            props: {span: 12},
                            style: {width: '150px'},
                            children: [
                                {
                                    type: 'inputNumber',
                                    field: 'limit',
                                    title: '最大上传数',
                                    value: 1,
                                    props: {min: 0, max: 10},
                                }
                            ]
                        }
                    ]
                },
            ]
        );
    }
};
